package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MovieRecAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.databinding.ActivitySearchBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResSetBean;

public class SearchActivity extends BaseAc<ActivitySearchBinding> {

    private static String key;
    private List<StkResBean> listShow = new ArrayList<>();
    private MovieRecAdapter searchAdapter;
    private RecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSearchBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivClearRecord.setOnClickListener(this);

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        mDataBinding.rvRecordList.setLayoutManager(layoutManager);
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);

        mDataBinding.rvSearchList.setLayoutManager(new GridLayoutManager(mContext, 3));
        searchAdapter = new MovieRecAdapter();
        mDataBinding.rvSearchList.setAdapter(searchAdapter);
        searchAdapter.setOnItemClickListener(this);

        mDataBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                key = mDataBinding.etSearch.getText().toString().trim();
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    if (key.isEmpty()) {
                        ToastUtils.showShort(getString(R.string.please_input_text));
                        return true;
                    }
                    getData();
                    saveRecord();
                    getRecord();
                }
                return false;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivClearRecord://清空历史记录
                List<String> listSave = new ArrayList<>();
                AppSpUtils.saveSearchRecordList(listSave);
                getRecord();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == recordAdapter) {
            key = recordAdapter.getItem(position);
            mDataBinding.etSearch.setText(key);
            getData();
            saveRecord();
            getRecord();
        } else {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            MovieDetailActivity.stkResBean = bean;
            startActivity(MovieDetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        getRecord();
    }

    private void getData() {
        listShow.clear();
        StkResApi.getTagResourceSetsList(null, AppConfig.SEARCH_DATA, key, null, false, new IReqRetCallback<StkResSetBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable StkResSetBean result) {
                if (success) {
                    if (result.articleList.size() > 0) {
                        listShow.addAll(result.articleList);
                    }
                }
                if (listShow.size() > 0) {
                    searchAdapter.setList(listShow);
                    mDataBinding.rvSearchList.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.rvSearchList.setVisibility(View.GONE);
                }
            }
        });
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        listSave.add(key);
        List<String> listSp = AppSpUtils.getSearchRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.remove(key);
            listSp.add(0, key);
            AppSpUtils.saveSearchRecordList(listSp);
        } else {
            AppSpUtils.saveSearchRecordList(listSave);
        }
    }

    private void getRecord() {
        List<String> listSp = AppSpUtils.getSearchRecordList();
        if (listSp != null && listSp.size() > 0) {
            recordAdapter.setList(listSp);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        }
    }

}